﻿Ext.require([
    'Ext.grid.*',
    'Ext.data.*',
    'Ext.tab.*',
    'Ext.window.*',
    'Ext.tip.*',
    'Ext.layout.container.Border'
    ]);

Ext.onReady(function () {

    Ext.define('Call', {
        extend: 'Ext.data.Model',
        fields: [
	           { name: 'Id', type: 'int' },
	           { name: 'Subject', type: 'string' },
               { name: 'Timestamp', type: 'date' },
               { name: 'Author.Name', type: 'string' },
               { name: 'Author.ParentBusiness.Name', type: 'string' }
	        ],
        idProperty: 'Id'
    });


    var dsCall = Ext.create('Ext.data.Store', {
        autoLoad: true,
        autoSync: true,
        model: 'Call',
        storeId: 'Id',
        proxy: {
            type: 'rest',
            url: '../Call/GetAll',
            reader: {
                type: 'json',
                root: 'calls'
            },
            writer: {
                type: 'json'
            }
        }
    });



    var grid = Ext.create('Ext.grid.Panel', {
        store: dsCall,
        stateful: true,
        collapsible: true,
        multiSelect: true,
        stateId: 'stateGrid',
        fit: true,
        columns: [
            {
                text: 'Código',
                flex: 0,
                sortable: true,
                width: 80,
                dataIndex: 'Id',
                renderer: function (val) { return '<div class="idCol">' + val.toString() + '</div>'; }
            },
            {
                text: 'Assunto',
                sortable: true,
                width: 400,
                dataIndex: 'Subject'
            },
            {
                text: 'Data',
                sortable: true,
                dataIndex: 'Timestamp'
            },
            {
                text: 'Requisitante',
                sortable: true,
                dataIndex: 'Author.Name'
            },
            {
                text: 'Empresa',
                width: 250,
                sortable: true,
                dataIndex: 'Author.ParentBusiness.Name'
            },
            {
                text: 'Abrir',
                width: 60,
                renderer: function (val) { return '<input type="image" src="../../Content/Themes/Aliasis/zoom.png" value="Visualizar" onClick="openCall(' + val + ');"/>'; },
                align: 'center',
                dataIndex: 'Id'
            },
            {
                text: 'Ver',
                width: 60,
                renderer: function (val) { return '<input id="show-btn" type="image" src="../../Content/Themes/Aliasis/application_view_tile.png" value="Pré-Visualizar" onClick="openPreview(' + val + ');"/>'; },
                align: 'center',
                dataIndex: 'Id'
            }
        ],
        height: 350,
        width: "100%",
        title: 'Chamados',
        renderTo: 'grid-calls',

        dockedItems: [{
            xtype: 'toolbar',
            items: [{
                text: 'Criar chamado',
                iconCls: 'iconAdd',
                handler: function () {
                    window.location = "Call/Create";
                }
            }]
        }],

        viewConfig: {
            stripeRows: true,
            enableTextSelection: true
        }
    });

});